package com.isea.pomodoro;

import java.awt.Component;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

public class MouseMoveListener extends MouseAdapter implements MouseMotionListener {
	private Component parentComponent;
	private Component dragCom;
	private Point offset;

	public synchronized void install(Component comp,Component dragCom) {  
        uninstall();  
        parentComponent = comp;
        this.dragCom = dragCom;  
        dragCom.addMouseListener(this);  
        dragCom.addMouseMotionListener(this);  
    }  
  
    public synchronized void uninstall() {  
        if (dragCom != null) {  
            dragCom.removeMouseListener(this);  
            dragCom.removeMouseMotionListener(this);  
            dragCom = null;  
        }  
    }  
    
	@Override
	public void mousePressed(MouseEvent e) {
		if (e.getSource() == dragCom)  
            offset = e.getPoint();
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		if (e.getSource() != dragCom)  
            return;  
        final int x = parentComponent.getX();  
        final int y = parentComponent.getY();  
        final Point lastAt = e.getPoint();  
        parentComponent.setLocation(x + lastAt.x - offset.x, y + lastAt.y - offset.y); 
	}
	
}
